package com.wellview.template.push;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;

import com.alibaba.sdk.android.push.MessageReceiver;
import com.alibaba.sdk.android.push.notification.CPushMessage;
import com.google.gson.Gson;
import com.wellview.template.R;
import com.wellview.template.bean.AliyNotifyBean;
import com.wellview.template.logger.Logger;
import com.wellview.template.utils.Utils;
import com.wellview.template.view.ActionHandleActivity;
import com.wellview.template.view.LoginActivity;
import com.wellview.template.view.MainActivity;

import java.net.URL;
import java.util.Map;

/**
 * Created by Administrator on 2017/12/6 0006.
 */

public class MyMessageReceiver extends MessageReceiver {

    private String var4;

    @Override
    public void onHandleCall(Context context, Intent intent) {
        String var4 = intent.getStringExtra("body");
        Logger.i(TAG,"var4 "+var4);
        this.var4 = var4;
        super.onHandleCall(context, intent);
    }

    private String TAG = getClass().getSimpleName();
    @Override
    public void onNotification(Context context, String title, String summary, Map<String, String> extraMap) {
        // TODO 处理推送通知
        if (!TextUtils.isEmpty(var4)){
            AliyNotifyBean aliyNotifyBean = new Gson().fromJson(var4, AliyNotifyBean.class);
            Logger.w(TAG, "Receive notification, title: " + title + ", summary: " + summary + ", extraMap: " + extraMap);
            NotifyTarget26(context,aliyNotifyBean);
        }
    }
    @Override
    public void onMessage(Context context, CPushMessage cPushMessage) {
        Logger.w(TAG, "onMessage, messageId: " + cPushMessage.getMessageId() + ", title: " + cPushMessage.getTitle() + ", content:" + cPushMessage.getContent());
    }
    @Override
    public void onNotificationOpened(Context context, String title, String summary, String extraMap) {
        Logger.w(TAG, "onNotificationOpened, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap);
    }
    @Override
    protected void onNotificationClickedWithNoAction(Context context, String title, String summary, String extraMap) {
        Logger.w(TAG, "onNotificationClickedWithNoAction, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap);
    }
    @Override
    protected void onNotificationReceivedInApp(Context context, String title, String summary, Map<String, String> extraMap, int openType, String openActivity, String openUrl) {
        Logger.w(TAG, "onNotificationReceivedInApp, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap + ", openType:" + openType + ", openActivity:" + openActivity + ", openUrl:" + openUrl);
    }
    @Override
    protected void onNotificationRemoved(Context context, String messageId) {
        Logger.w(TAG, "onNotificationRemoved");
    }


    private void NotifyTarget26(Context context,AliyNotifyBean aliyNotifyBean) {
        NotificationManager manager =  (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= 26){
            NotificationChannel channel = new NotificationChannel("wst",
                    context.getResources().getString(R.string.app_name),
                    NotificationManager.IMPORTANCE_HIGH);
            manager.createNotificationChannel(channel);
            Notification.Builder builder = new Notification.Builder(context,"wst");
            builder.setChannelId(channel.getId());
            builder.setContentText(aliyNotifyBean.getContent());
            builder.setContentTitle(aliyNotifyBean.getTitle());
            builder.setSmallIcon(R.drawable.app);
            builder.setAutoCancel(true);
            builder.setNumber(Integer.MAX_VALUE);
            Intent intent = new Intent(context, ActionHandleActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
            String url = aliyNotifyBean.getUrl();
            if (!TextUtils.isEmpty(url)){
                intent.putExtra("action",ActionHandleActivity.ACTION_LOAD_URL);
                intent.putExtra("url",url);
            }
//            if (Utils.is(activity)){
//                try {
//                    Class<Activity> aClass = (Class<Activity>) Class.forName(activity);
//                    intent.setClass(context,aClass);
//                    ComponentName componentName = intent.resolveActivity(context.getPackageManager());
//                    if (componentName != null){
//
//                    }else {
//
//                    }
//                } catch (ClassNotFoundException e) {
//                    intent.putExtra("activity","");
//                    e.printStackTrace();
//                }
//            }
//            stackBuilder.addParentStack(MainActivity.class);
//            stackBuilder.addNextIntent(intent);
//            PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,
//                    PendingIntent.FLAG_UPDATE_CURRENT);

            PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
            builder.setContentIntent(pendingIntent);
            Notification notification = builder.build();
            if (aliyNotifyBean.getRemind() == 3){
                notification.defaults = Notification.DEFAULT_SOUND & Notification.DEFAULT_VIBRATE;
            } else if (aliyNotifyBean.getRemind() == 2) {
                notification.defaults = Notification.DEFAULT_VIBRATE;
            }
            else if (aliyNotifyBean.getRemind() == 1) {
                notification.defaults = Notification.DEFAULT_SOUND;
            }
            manager.notify(aliyNotifyBean.getMsg_id(), notification);
        }else {


        }
    }
}